package game;

import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;

import main.Res;

public class Map {
	private Obstruction obstructions[];
	private int rows;
	private int cols;
	
	private int width;
	private int height;
	
	private static int CELL_SIZE = 25;
	private TiledLayer baseLayer;
	private TiledLayer obstructionLayer;

	public Map(int[][] matrix) {

		this.rows = matrix.length;
		this.cols = matrix[0].length;
		
		this.width = cols * CELL_SIZE;
		this.height = rows * CELL_SIZE;
		
		InitTiledLayers(matrix);
//		InitObtructions(matrix);
	}

	private void InitTiledLayers(int[][] matrix) {
		baseLayer = new TiledLayer(rows, cols, Res.get().imgBackground, CELL_SIZE, CELL_SIZE);
		obstructionLayer = new TiledLayer(rows, cols, Res.get().imgBackground, CELL_SIZE, CELL_SIZE);
		
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				baseLayer.setCell(i, j, 1);
				obstructionLayer.setCell(i, j, matrix[j][i]);
			}
		}
	}
	
	private void InitObtructions(int[][] matrix) {
		int count1 = 0;
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if (matrix[i][j] > 1) {
					count1++;
				}
			}
		}

		this.obstructions = new Obstruction[count1];

		int count2 = 0;
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				switch (matrix[i][j]) {
				case 2:
					obstructions[count2++] = new SandBag();
					break;
				case 3: // Wall left
					obstructions[count2++] = new Wall(Wall.LEFT);
					break;
				case 4: // Wall right
					obstructions[count2++] = new Wall(Wall.RIGHT);
					break;
				case 5: // Wall top
					obstructions[count2++] = new Wall(Wall.TOP);
					break;
				case 6: // Wall bottom
					obstructions[count2++] = new Wall(Wall.BOTTOM);
					break;
				}
			}
		}
	}

	public void addToLayer(LayerManager layer) {
		layer.append(obstructionLayer);
		layer.append(baseLayer);		
	}
	
	public int getWidth(){
		return this.width;
	}
	public int getHeight(){
		return this.height;
	}
}
